package com.tt.controller;

import com.tt.common.page.QueryInfo;
import com.tt.common.page.TtResult;
import com.tt.pojo.Item;
import com.tt.pojo.ItemParamItem;
import com.tt.pojo.vo.CatVo;
import com.tt.service.IGoodsCatService;
import com.tt.service.ItemParamItemService;
import com.tt.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author 青青
 */
@Controller
@RequestMapping("/goods")
public class ItemController
{

    private String prefix = "goods";

    @Autowired
    private ItemService itemService;

    @Autowired
    private IGoodsCatService goodsCatService;

    @Autowired
    private ItemParamItemService paramItemService;


    @GetMapping("/goodPage")
    public String goodPage()
    {
        return prefix + "/goods";
    }

    @GetMapping("/addGoodsPage")
    public String addGoodsPage(){return prefix + "/add-goods";}

    /**
     * 到编辑页面
     * @param model
     * @param itemId
     * @return
     */
    @GetMapping("/editGoodsPage")
    public String editGoodsPage(Model model,Long itemId)
    {
        Item itemById = itemService.getItemById(itemId);
        CatVo catVo = goodsCatService.getCatVo(itemById.getCid());

        //查找改商品对应的规格，注意 如果该商品没有规格，则查找改商品分类对应的规格
        ItemParamItem paramItem = paramItemService.getItemParamItem(itemById);
        model.addAttribute("item",itemById);
        model.addAttribute("catVo",catVo);
        model.addAttribute("paramItem",paramItem);
        return prefix + "/edit-goods";
    }

    /**
     * 选择商品类目页面
     * @return
     */
    @GetMapping("/itemCat")
    public String itemCat(){return prefix + "/goods-cat";}

    /**
     * 根据商品ID查找商品
     * @param itemId
     * @return
     */
    @RequestMapping("/item/{itemId}")
    @ResponseBody
    public Item getItemById(@PathVariable Long itemId)
    {
        Item tbItem = itemService.getItemById(itemId);
        return tbItem;
    }

    /**
     * 获取商品列表
     * @param queryInfo
     * @return
     */
    @RequestMapping("/getItemList")
    @ResponseBody
    public TtResult getItemList(QueryInfo queryInfo)
    {

        TtResult itemList = itemService.getItemList(queryInfo);
        return itemList;
    }

    @RequestMapping("/addGoods")
    @ResponseBody
    public TtResult addGoods(Item item)
    {
        Integer integer = itemService.addItem(item);
        return TtResult.ok(integer);
    }


    @RequestMapping("/updateGoods")
    @ResponseBody
    public TtResult updateGoods(Item item)
    {
        Integer integer = itemService.updateItem(item);
        return TtResult.ok(integer);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/delGoods")
    @ResponseBody
    public TtResult delGoods(@RequestParam("ids[]") Long[] ids)
    {
        List<Long> integers = Arrays.asList(ids.clone());
        Integer integer = itemService.delItem(integers);
        return TtResult.ok(integer);
    }


}
